from PySide6.QtWidgets import QFileDialog
from typing import List, Optional
from ui.components.base.base_dialog import TgBaseDialog

class TgFileDialog(TgBaseDialog):
    def __init__(self, parent=None):
        super().__init__(parent)

    def get_open_file_name(self, title: str = "Open File", extensions: Optional[List[str]] = None) -> str:
        file_filter = "All Files (*)"
        if extensions:
            ext_str = " ".join([f"*.{ext}" for ext in extensions])
            custom_filter = f"Custom Files ({ext_str})"
            file_filter = f"{custom_filter};;{file_filter}"

        file_path, _ = QFileDialog.getOpenFileName(self, title, filter=file_filter)
        return file_path

    def get_existing_directory(self, title: str = "Select Directory") -> str:
        directory = QFileDialog.getExistingDirectory(self, title)
        return directory

    def get_save_file_name(
        self,
        title: str = "Save File",
        extensions: Optional[List[str]] = None,
        default_name: str = ""
    ) -> str:
        """弹出保存文件对话框。

        - 注释用中文；日志如需添加请使用英文。
        - extensions 例如 ["csv", "txt"]。
        - default_name 为建议的默认文件名（可含路径）。
        """
        file_filter = "All Files (*)"
        if extensions:
            ext_str = " ".join([f"*.{ext}" for ext in extensions])
            custom_filter = f"Custom Files ({ext_str})"
            file_filter = f"{custom_filter};;{file_filter}"

        file_path, _ = QFileDialog.getSaveFileName(
            self,
            title,
            default_name,
            filter=file_filter
        )
        return file_path